package com.itheima.controller;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.util.SMSUtils;
import com.itheima.util.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Random;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

	@Autowired
	private JedisPool jedisPool;

	@RequestMapping("/send")
	public Result send(String phoneNumber) {
		//随机生成4位验证码
		String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
		System.out.println("生成的预约验证码是："+validateCode);
		try {
			SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber,validateCode);
		} catch (ClientException e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}
		//验证码发送成功后，存入redis，时间5分钟,过后失效
		jedisPool.getResource().setex((phoneNumber+RedisMessageConstant.SENDTYPE_ORDER),300,validateCode);
		return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
	}

	@RequestMapping("/send4Login")
	public Result send4Login(String phoneNumber) {
		//随机生成4位验证码
		String validateCode = ValidateCodeUtils.generateValidateCode4String(4);
		System.out.println("生成的登录验证码是："+validateCode);
		try {
			SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, phoneNumber,validateCode);
		} catch (ClientException e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
		}
		//验证码发送成功后，存入redis，时间5分钟,过后失效
		jedisPool.getResource().setex((phoneNumber+RedisMessageConstant.SENDTYPE_LOGIN),300,validateCode);
		return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
	}

}
